package com.example.textswitchtestdemo01.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.NewsInfo;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.example.textswitchtestdemo01.util.RetrofitUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EighthActivity extends AppCompatActivity {
    private TextView tv_title;
    private TabLayout tl_eighth;
    private ViewPager vp_eighth;
    private ArrayList<View> views = new ArrayList<>();
    private String[] titles = {"推荐", "热点", "咨询", "口碑"};
    private ListView lv_eighth_vp;
    private List<NewsInfo.DataBean> listRecommend = new ArrayList<>();
    private List<NewsInfo.DataBean> listNews = new ArrayList<>();
    private List<NewsInfo.DataBean> listHot = new ArrayList<>();
    private List<NewsInfo.DataBean> listAwesome = new ArrayList<>();
    private NewsAdater newsAdater;
    private NewsAdater newsAdater2;
    private NewsAdater newsAdater3;
    private NewsAdater newsAdater4;
    private ListView lv_eighth_vp2;
    private ListView lv_eighth_vp3;
    private ListView lv_eighth_vp4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eighth);
        initView();
        InitToolbar.initToolbar(this, "第八页", true);

        getData();
    }

    private void getData() {
        RetrofitUtil.getAllNew(new Callback<NewsInfo>() {
            @Override
            public void onResponse(Call<NewsInfo> call, Response<NewsInfo> response) {
                List<NewsInfo.DataBean> data = response.body().getData();
                if (data != null && !data.isEmpty()) {
                    listRecommend.clear();
                    listHot.clear();
                    listNews.clear();
                    listAwesome.clear();

                    for (NewsInfo.DataBean b : data) {
                        int status = b.getStatus();
                        if (0 == status) {
                            listRecommend.add(b);
                        } else if (1 == status) {
                            listHot.add(b);
                        } else if (2 == status) {
                            listNews.add(b);
                        } else {
                            listAwesome.add(b);
                        }
                    }
                }
                Toast.makeText(EighthActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();

                newsAdater.notifyDataSetChanged();
                newsAdater2.notifyDataSetChanged();
                newsAdater3.notifyDataSetChanged();
                newsAdater4.notifyDataSetChanged();

            }

            @Override
            public void onFailure(Call<NewsInfo> call, Throwable t) {
                Toast.makeText(EighthActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @SuppressLint("InflateParams")
    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tl_eighth = (TabLayout) findViewById(R.id.tl_eighth);
        vp_eighth = (ViewPager) findViewById(R.id.vp_eighth);

        LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
        views.add(layoutInflater.inflate(R.layout.item_vp_eighth, null));
        views.add(layoutInflater.inflate(R.layout.item_vp_eighth, null));
        views.add(layoutInflater.inflate(R.layout.item_vp_eighth, null));
        views.add(layoutInflater.inflate(R.layout.item_vp_eighth, null));

        tl_eighth.setupWithViewPager(vp_eighth);
        vp_eighth.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return (view == object);
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(views.get(position));
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(views.get(position));
                return views.get(position);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });


        lv_eighth_vp = (ListView) views.get(0).findViewById(R.id.lv_eighth_vp);
        newsAdater = new NewsAdater(getApplicationContext(), listRecommend);
        lv_eighth_vp.setAdapter(newsAdater);

        lv_eighth_vp2 = (ListView) views.get(1).findViewById(R.id.lv_eighth_vp);
        newsAdater2 = new NewsAdater(getApplicationContext(), listHot);
        lv_eighth_vp2.setAdapter(newsAdater2);

        lv_eighth_vp3 = (ListView) views.get(2).findViewById(R.id.lv_eighth_vp);
        newsAdater3 = new NewsAdater(getApplicationContext(), listNews);
        lv_eighth_vp3.setAdapter(newsAdater3);

        lv_eighth_vp4 = (ListView) views.get(3).findViewById(R.id.lv_eighth_vp);
        newsAdater4 = new NewsAdater(getApplicationContext(), listAwesome);
        lv_eighth_vp4.setAdapter(newsAdater4);

        lv_eighth_vp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpWithData(position,listRecommend);
            }
        });

        lv_eighth_vp2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpWithData(position,listHot);
            }
        });

        lv_eighth_vp3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpWithData(position,listNews);
            }
        });

        lv_eighth_vp4.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpWithData(position,listAwesome);
            }
        });
    }

    private void jumpWithData(int position, List<NewsInfo.DataBean> list) {
        Intent intent = new Intent(EighthActivity.this, EighthDetailActivity.class);
        intent.putExtra("id", list.get(position).getId());
        startActivity(intent);
    }
}
